package com.gxuwz.weather.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gxuwz.weather.R;
import com.gxuwz.weather.model.VersionInfo;

public class UpdateDialog extends Dialog {
    private VersionInfo versionInfo;
    private OnUpdateListener listener;
    private TextView tvUpdateTitle;
    private TextView tvUpdateContent;
    private Button btnCancel;
    private Button btnUpdate;

    public interface OnUpdateListener {
        void onUpdate();
    }

    public UpdateDialog(@NonNull Context context, VersionInfo versionInfo, OnUpdateListener listener) {
        super(context);
//        super(context, R.style.Theme_AppCompat_Dialog); // 可自定义Dialog样式
        this.versionInfo = versionInfo;
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update);

        tvUpdateTitle = findViewById(R.id.tv_update_title);
        tvUpdateContent = findViewById(R.id.tv_update_content);
        btnCancel = findViewById(R.id.btn_cancel);
        btnUpdate = findViewById(R.id.btn_update);

        // 设置标题
        tvUpdateTitle.setText("发现新版本 v" + versionInfo.getVersionName());
        // 设置更新内容
        tvUpdateContent.setText(versionInfo.getUpdateContent());

        // 如果是强制更新，隐藏取消按钮
        if (versionInfo.isForceUpdate()) {
            btnCancel.setVisibility(View.GONE);
            setCancelable(false);
        }

        btnCancel.setOnClickListener(v -> dismiss());
        btnUpdate.setOnClickListener(v -> {
            if (listener != null) {
                listener.onUpdate();
            }
            dismiss();
        });
    }
}